package com.ncd.disease.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ncd.disease.domain.RetPatient;
import com.ncd.disease.domain.RetPatientInfo;
import com.ncd.disease.mapper.RetPatientInfoMapper;
import com.ncd.disease.mapper.RetPatientMapper;
import com.ncd.disease.param.RetPatientParam;
import com.ncd.disease.param.RetPatientQueryParam;
import com.ncd.disease.service.IRetPatientInfoService;
import com.ncd.disease.service.IRetPatientService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 患者基本信息Service业务层处理
 * 
 * @author zhouxin
 * @date 2024-03-07
 */
@Service
public class RetPatientServiceImpl extends ServiceImpl<RetPatientMapper,RetPatient> implements IRetPatientService
{
    @Resource
    private RetPatientMapper retPatientMapper;

    @Resource
    IRetPatientInfoService retPatientInfoService;

    @Resource
    RetPatientInfoMapper retPatientInfoMapper;

    /**
     * 查询患者基本信息
     * 
     * @param id 患者基本信息主键
     * @return 患者基本信息
     */
    @Override
    public RetPatient selectRetPatientById(Long id)
    {
        return this.getById(id);
    }

    /**
     * 查询患者基本信息列表
     * 
     * @param retPatient 患者基本信息
     * @return 患者基本信息
     */
    @Override
    public List<RetPatient> selectRetPatientList(RetPatientQueryParam retPatient)
    {
        QueryWrapper<RetPatient> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(retPatient.getPatName()),"t1.pat_name",retPatient.getPatName());
        wrapper.like(StrUtil.isNotBlank(retPatient.getPatIdCard()),"t1.pat_Id_card",retPatient.getPatIdCard());
        wrapper.eq(ObjectUtil.isNotNull(retPatient.getDiaId()),"t2.pati_dia_id",retPatient.getDiaId());
        wrapper.eq(ObjectUtil.isNotEmpty(retPatient.getAdmType()),"t2.pati_adm_type",retPatient.getAdmType());
        return this.baseMapper.selectRetPatientList(wrapper);
    }

    /**
     * 新增患者基本信息
     * 
     * @param retPatient 患者基本信息
     * @return 结果
     */
    @Override
    public int insertRetPatient(RetPatient retPatient)
    {
        retPatientMapper.insert(retPatient);
        RetPatientInfo retPatientInfo=new RetPatientInfo();
        retPatientInfo.setPatId(retPatient.getId());
        retPatientInfo.setPatiDiaId(retPatient.getPatiDiaId());
        retPatientInfo.setPatiAdmType(retPatient.getPatiAdmType());
        return retPatientInfoMapper.insert(retPatientInfo);
    }

    /**
     * 修改患者基本信息
     * 
     * @param param 患者基本信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRetPatient(RetPatientParam param)
    {
        retPatientInfoService.saveOrUpdate(param.getRetPatientInfo());
        return retPatientMapper.updateById(param.getRetPatient());
    }

}
